define(['config', 'text!app/module/system/productionDetail.html', 'laypage',
    'mydialog', 'autocomplete', 'jqGrid', 'common', 'laydate'
], function (config, viewHTML, laypage,
    mydialog, autocomplete, jqGrid, common, laydate) {
	
	var token = (cache.go("token") === 'undefined') ? 'none' : cache.go("token");

    var grid_url = "/api/produce";
    
    
    var grid_arr = new Array();
    
    var orderNo = Public.getRequestParam ("orderNo");

    
    var model = avalon.define({
    	$id : "productionDetail",
         // 查询条件 	
         query: {
        	 orderNo: "",
         },

         produceInfo:{
        	 ceva:"",
        	 comment:"",
        	 destination:"",
        	 examination:"",
        	 freightForwarding:"",
        	 id:"",
        	 orderNo:"",
        	 productDepartment:"",
        	 productionNo:"",
        	 qcDepartment:"",
        	 samDepartment:"",
        	 startingDate:"",
        	 status:"",
        	 uuid:""
         },
        
         statusArr: [],
         selected: [],
         roles: [],
         
         init: function () {
        	 model.initPage();
             model.addEvent();
             model.initGoods();
         },
         /**接收orders页面传的orderNo*/
         initPage:function(){
	        	
        		 
            	 $.ajax({
                     type: "POST",
                     url: "/api/produce/list",
                     data: {
                    	 "page":1,
                    	 "rows":1,
                         "orderNo": orderNo
                     },
                     beforeSend: function (xhr) {
                         xhr.setRequestHeader("X-AuthToken", token);
                     },
                     success: function (data) {
                    	 console.log("[data123123213]",data);
                    	 model.initproduce();
                         model.produceInfo = data.data;
                         console.log("[model.produceInfo]",model.produceInfo);
                         model.produceInfo.startingDate = Public.dateFormat(model.produceInfo.startingDate);
                         if(model.produceInfo.examination == 1){
                        	 $("#save").remove();
                         }
                        	$("#grid").jqGrid({
                        		     url:'api/produce/getDetailList',
                        		     postData:{
                        		    	 "orderNo":orderNo
                        		     },
                        		     datatype: "json",
                                     colModel: [
			                               {
			                                   name: "orderNo",
			                                   label: "订单号",
			                                   width: '8%',
			                               },
			                               {
			                                   name: "lotNo",
			                                   label: "批次号",
			                                   width: '8%',
			                               },
			                               {
			                                   name: "name",
			                                   label: "品名",
			                                   width: '10%',
			                               },
			                               {
			                                   name: "sku",
			                                   label: "代号（SKU）",
			                                   width: "7%"
			                               },
			                               {
			                                   name: "model",
			                                   label: "规格",
			                                   width: '10%',
			                               },
			                               {
			                                   name: "amount",
			                                   label: "总件",
			                                   width: '13%',
			                               },
			                               {
			                                   name: "already",
			                                   label: "已生产",
			                                   width: '13%',
			                               },
			                               {
			                                   name: "alreadyOut",
			                                   label: "已出库",
			                                   width: '13%',
			                               },
			                               {
			                                   name: "comment",
			                                   label: "备注",
			                                   width: '10%',
			                               }
			                           ],
			                           page: 1,
		                               rowNum: 10,
		                               rowList: [10, 20, 30],
		                               height: '150px',
		                               autowidth: true,
		                               pager: '#page',
		                               sortname: 'id',
		                               viewrecords: true,
		                               forceFit: false,
		                               rownumbers: true,
		                               multiselect: true,
		                               multiboxonly: true,
		                               sortorder: "desc",
		                               jsonReader: {
			                           	 	root:"data.list",
			                                records: "data.totalRow",
			                                total: "data.totalPage",
			                                repeatitems: false,
			                                id: "id"
		                                }
                        		 });
                     },
                     error: function (responseData, textStatus, errorThrown) {
                         alert("Failed: " + responseData);
                     }
                 });
         },
         initGoods:function(){
        	 $("#grid_d").jqGrid({
    		     url:'api/produce/getLoss',
    		     postData:{
    		    	 "orderNo":orderNo
    		     },
    		     datatype: "json",
                 colModel: [
                       {
                           name: "pn",
                           label: "物料编号",
                           width: '8%',
                           hidden:true
                       },
                       {
                           name: "name",
                           label: "物料名称",
                           width: '8%',
                       },
                       {
                           name: "model",
                           label: "规格型号",
                           width: '10%',
                       },
                       {
                           name: "unit",
                           label: "计量单位",
                           width: '10%',
                           formatter: function (value, options, rowData) {
                               if (value == "011") {
                                   return "件";
                               }
                           },
                           hidden:true
                       },
                       {
                           name: "consumption",
                           label: "数量",
                           
                           width: "7%"
                       }
                   ],
                   page: 1,
                   rowNum: 10,
                   rowList: [10, 20, 30],
                   height: '150px',
                   autowidth: true,
                   pager: '#page_d',
                   sortname: 'id',
                   viewrecords: true,
                   forceFit: false,
                   rownumbers: true,
                   multiselect: true,
                   multiboxonly: true,
                   sortorder: "desc",
                   jsonReader: {
                   	 	root:"data",
                        records: "data.totalRow",
                        total: "data.totalPage",
                        repeatitems: false,
                        id: "id"
                    }
    		 });
         },
         
         addEvent: function () {
             //提交表单
             $(".fr").on("click","#save",function(e){
            	 console.log(model.produceInfo.examination)
	        	 if(model.produceInfo.examination ==  1){
	        		 dialog({
	                     title: "保存失败",
	                     content: "已审核的生产任务单禁止修改！",
	                     okValue: "确定",
	    	        	 ok: function (){
	    	        	 }
	            	 }).showModal();
	             }else{
		             for (var i = 0; i < grid_arr.length; i++) {
		            		 grid_arr[i].id = "";
					 }
		        	 $.ajax({
		        		 contentType: "application/json;charset=utf-8",
		                 type: "POST",
		                 url: "/api/produce/saveProduction",
		                 data:JSON.stringify(model.produceInfo.$model),
		                 beforeSend: function (xhr) {
		                     xhr.setRequestHeader("X-AuthToken", token);
		                 },
		                 success: function () {
		                	 dialog({
			                     title: "保存成功",
			                     content: "保存成功！",
			                     okValue: "确定",
			    	        	 ok: function (){
			    	        		 location.replace('index.html#!/109');
			    	        	 }
			            	 }).showModal();
		                 },
		                 error: function (responseData, textStatus, errorThrown) {
		                     alert("Failed: " + responseData);
		                 }
		             });
	             }
             });
        	 //返回
             $(".fr").on("click", "#close", function (e){
            	 console.log('1111111111111111111111');
                 top.topManager.closePage();
                 top.topManager.openPage({
          			id:"produceManage",
          			title:"生产管理",
          			href:'index.html#!/109',
          			reload:true
          		 })
             });

             
         },
         initproduce:function(){
        	 model.produceInfo={
        			 ceva:"",
                	 comment:"",
                	 destination:"",
                	 examination:"",
                	 freightForwarding:"",
                	 id:"",
                	 orderNo:"",
                	 productDepartment:"",
                	 productionNo:"",
                	 qcDepartment:"",
                	 samDepartment:"",
                	 startingDate:"",
                	 status:"",
                	 uuid:""
			         }
			     },
			});
    
    // 定义视图模块
    var viewmod = {
        viewtSrc: viewHTML,
        viewInit: function () {
            model.init();
        }, // 模块view加载完成后的回调方法，在刷新（F5）时可能调用两次
        viewDestroy: function () {} // 切换菜单时销毁原来的view
    };
    return viewmod;
	
	
});